package com.kaifamiao.bytestreams;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
 * OutputStream中的方法:
 * 1、void close() 关闭字节输出流
 * 2、abstract void write( int b )
 *         将参数值写入到当前的字节输出流中
 * 3、void write(byte[] bytes)
 *         将参数对应数组中所有字节写入到当前的字节输出流中
 * 4、void write(byte[] bytes, int offset, int length)
 *         将指定数组bytes中从offset开始的length个字节写入到当前字节输出流中
 */
public class OutputStreamTest1 {

    public static void main(String[] args) throws IOException {
        String pathname = "./out.txt";
        OutputStream out = new FileOutputStream( pathname );

        // 向字节输出流中写入单个字节
        out.write( 65 );
        out.write( 'B' );
        out.write( '\u0043');
        out.write( '王' );
        out.write( '\n' );

        byte[] bytes = "今天天气好晴朗".getBytes(StandardCharsets.UTF_8);
        // 向字节输出流中写入整个字节数组中的内容
        out.write( bytes );
        out.write( '\n' );
        // 将字节数组中部分字节写入到字节输出流中
        out.write( bytes, 12,9 );

        out.close();
    }

}
